var app=getApp()
const datautil1 = require("../../utils/data.js");
const datautil2 = require("../../utils/util.js");
var url=app.globalData.url
var url1 = app.globalData.url1
var orderId=""
var data2 = '' //个人时间
var data4 = '' //个人日期
var reason=''
var address={}
Page({

  /**
   * 页面的初始数据
   */
  data: {
      flag:true,
      names:"返修",
    hours1: [{ time: "上午", timeframe: "8:00~10:00" }, { time: "中午", timeframe: "11:00~14:00" }, { time: "下午", timeframe: "14:00~17:00" }, { time: "晚上", timeframe: "17:00~20:00" }],
    optionsTime:"上门时间"
  },
back(){
  wx.navigateBack()
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that=this
   orderId=options.orderId
    that.getDates()
    that.getDefaultAddr()
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc)
        that.setData({
          winY: calc,
          orderId:orderId
        });
      }
    });
  },
  selectTime11(){
    this.showPopup()
  },
  showPopup() {
    let popupComponent = this.selectComponent('.J_Popup');
    popupComponent && popupComponent.show();
  },
  hidePopup() {
    let popupComponent = this.selectComponent('.J_Popup');
    popupComponent && popupComponent.toggle(false);
    // popupComponent && popupComponent.hide();
  },

  popupTap() {
    let popupComponent = this.selectComponent('.J_Popup');

    popupComponent && popupComponent.toggle(false);
  },

  // 选择日期
  selectDate(e) {
    const that = this
    console.log(e)
    var index = e.currentTarget.dataset.index
    //var time=that.data.hours
    console.log("that.data.times: ")
    console.log(e.currentTarget.dataset.dates)

    data2 = e.currentTarget.dataset.dates
    that.setData({
      dateIndex: index,
      datess: data2
      // times: time[index].timeFrames
    })

  },
  // 选择时间
  selectTime(e) {
    const that = this
    console.log(e)
    data4 = e.currentTarget.dataset.time
    that.setData({
      timeIndex: e.currentTarget.dataset.index,
      timess: data4
    })
  },
  getReason(e){
    reason=e.detail.value
  },
  getDates() {
    const that = this
    var time = datautil2.formatTime(new Date(), true);

    //获取一周的时间戳
    var toda = datautil1.getDates(5, time)
    console.log(toda)
    that.setData({
      datas: toda,
      currentDate: datautil2.formatTime(new Date(), false)
    })
  },
  // 确认个人的时间
  configTime() {
    const that = this
    if (data2.length == 0 || data4.length == 0) {
      wx.showToast({
        title: '选择日期时间',
        icon: "loading",
        duration: 1000
      })
    } else {
      that.setData({
        optionsTime:data2+" "+data4
      })
      this.hidePopup()
       
    }
   

  },
  submitbx(){
    const that=this
    // wx.navigateTo({
    //   url: '../details/details',
    // })
    if (reason.length==0){
      wx.showToast({
        title: '请输入返修原因',
        icon:"loadinng",
        duration:1000
      })
    }else{
    var user = wx.getStorageSync("user")
    wx.request({
      url: url1+'/AfterSale/Sale',
      data:{
        usesAccount: user.wxUsersAccount,
        orderId:orderId,//订单号
        aName: address.addressName,//返修地址联系人
        aPhone: address.addressPhone,//返修地址联系电话
        aLocal: address.addressLocation,//返修上门地址
        aTime:data2+data4,//返修上门时间
        cause:reason//返修原因
      },
      header:{
        "content-type":"application/json"
      },success(res){
        console.log(res.data)
          if(res.data.code==500){
          wx.showToast({
            title: '服务器繁忙',
            icon:"loading",
            duration:1000
          })
          }else{
            app.globalData.currentTap = 3
            wx.navigateBack(1)
          }
      },
    })
    }
  },

  selectAddr() {
    var that = this;

    wx.getSetting({
      success: (res) => {
        console.log(res);
        console.log(res.authSetting['scope.userLocation']);
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) { //非初始化进入该页面,且未授权
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权，否则下单等功能将无法使用',
            success: function (res) {
              if (res.cancel) {
                console.info("授权失败");

              } else if (res.confirm) {
                //village_LBS(that);
                wx.openSetting({
                  success: function (data) {
                    console.log(data);
                    if (data.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 5000
                      })
                      //再次授权，调用getLocationt的API

                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'success',
                        duration: 5000
                      })
                    }
                  }
                })
              }
            }
          })
        }
      }
    })

    wx.chooseLocation({
      success: function (s) {
        console.log(s)
        address.addressLocation = s.address
    
        that.setData({
          location: s.address
        })
      },
    })

  },
  getName(e) {
    console.log(e)
    address.addressName = e.detail.value
  },
  getPhone(e) {
    address.addressPhone = e.detail.value
  },
  getAddrDoornumber(e) {
    address.addressDoorNumber = e.detail.value
  },
  // 获取默认地址
  getDefaultAddr() {
    const that = this
    var user = wx.getStorageSync("user")
    wx.request({
      url: url + '/address/default_address',
      method: "get",
      header: {
        "content-type": "application/json"
      },
      data: {
        usersTel: user.wxUsersAccount
      },
      success(res) {
        console.log(res.data)
        if (res.data.code == 1) {
          address = res.data.data.address
          that.setData({
            addressName: address.addressName,
            addressTel: address.addressPhone,
            location: address.addressLocation
          })
        } else {

        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})